package com.bruce.rentHouse.exception;

import com.bruce.rentHouse.restFul.ResultEntity;
import com.bruce.rentHouse.restFul.ResultManger;
import com.bruce.rentHouse.restFul.ResultStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by bruce on 18/2/3 -- 17:13
 * ____    ____  ___      .__   __.   _______        __   __   _______
 * \   \  /   / /   \     |  \ |  |  /  _____|      |  | |  | |   ____|
 * \   \/   / /  ^  \    |   \|  | |  |  __        |  | |  | |  |__
 * \_    _/ /  /_\  \   |  . `  | |  | |_ | .--.  |  | |  | |   __|
 * |  |  /  _____  \  |  |\   | |  |__| | |  `--'  | |  | |  |____
 * |__| /__/     \__\ |__| \__|  \______|  \______/  |__| |_______|
 */
@ControllerAdvice
public class ExceptionHandle {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultEntity handle(Exception e) {
        if (e instanceof GlobalException) {
            return ResultManger.error(((GlobalException) e).getCode(), e.getMessage());
        } else {
            return ResultManger.error(ResultStatus.EXCEPTION, e.getMessage());
        }
    }
}
